﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ft_module_parser
{
    class pvfield_filter
    {
        public string ac_file = "";
        public string ft_file = "";
        public string output = "";
        public List<pv_field_raw> pv_ac = new List<pv_field_raw>();
        public List<pv_field_raw> pv_ft = new List<pv_field_raw>();
        public List<pv_field_raw> pv_dlc = new List<pv_field_raw>();
        public pvfield_filter(string ft_file, string output)
        {
            this.ft_file = ft_file;
            this.output = output;
        }




        public void filter()
        {
            StreamReader sr = new StreamReader(ft_file);
            StreamReader srdlc = new StreamReader("pv_field_dlc.txt");
            StreamReader srac = new StreamReader("pv_field_orig.txt");
            StreamWriter outp = new StreamWriter(output);

            while (srac.Peek() > -1)
            {
                string line = srac.ReadLine();
                string[] line2 = line.Split('.');
                string[] line3 = line2[0].Split('_');

                int pvid = int.Parse(line3[1]);

                var curRawPv = pv_ac.Where(c => c.pvId == pvid).FirstOrDefault();
                if (curRawPv == null)
                {
                    pv_ac.Add(new pv_field_raw(pvid));
                    curRawPv = pv_ac.Where(c => c.pvId == pvid).FirstOrDefault();
                    curRawPv.rawrows.Add(line);
                } else
                {
                    curRawPv.rawrows.Add(line);
                }
            }

            while (srdlc.Peek() > -1)
            {
                string line = srdlc.ReadLine();
                string[] line2 = line.Split('.');
                string[] line3 = line2[0].Split('_');

                int pvid = int.Parse(line3[1]);

                var curRawPv = pv_dlc.Where(c => c.pvId == pvid).FirstOrDefault();
                if (curRawPv == null)
                {
                    pv_dlc.Add(new pv_field_raw(pvid));
                    curRawPv = pv_dlc.Where(c => c.pvId == pvid).FirstOrDefault();
                    curRawPv.rawrows.Add(line);

                }
                else
                {
                    curRawPv.rawrows.Add(line);
                }
            }

            while (sr.Peek() > -1)
            {
                string line = sr.ReadLine();
                string[] line2 = line.Split('.');
                string[] line3 = line2[0].Split('_');

                int pvid = int.Parse(line3[1]);

                var curRawPv = pv_ft.Where(c => c.pvId == pvid).FirstOrDefault();
                if (curRawPv == null)
                {
                    pv_ft.Add(new pv_field_raw(pvid));
                    curRawPv = pv_ft.Where(c => c.pvId == pvid).FirstOrDefault();
                    curRawPv.rawrows.Add(line);
                } else
                {
                    curRawPv.rawrows.Add(line);
                }
                
            }

            for (int ai =0; ai < 999; ai++)
            {
                var i = pv_dlc.Where(c => c.pvId == ai).FirstOrDefault();
                if (i != null)
                {
                    var search_ac = pv_ac.Where(c => c.pvId == i.pvId).FirstOrDefault();
                    if (search_ac != null)
                    {
                        foreach (var u in search_ac.rawrows)
                        {
                            outp.WriteLine(u);
                        }
                    }
                    else
                    {
                        foreach (var u in i.rawrows)
                        {
                            //if (!u.Contains("auth_3d"))
                            outp.WriteLine(u);
                        }
                    }
                } else
                {
                    var ft = pv_ft.Where(c => c.pvId == ai).FirstOrDefault();
                    if (ft != null)
                    {
                        var search_ac = pv_ac.Where(c => c.pvId == ft.pvId).FirstOrDefault();
                        if (search_ac != null)
                        {
                            foreach (var u in search_ac.rawrows)
                            {
                                outp.WriteLine(u);
                            }
                        }
                        else
                        {
                            foreach (var u in ft.rawrows)
                            {
                                //if (!u.Contains("auth_3d"))
                                outp.WriteLine(u);
                            }
                        }
                    }
                }
            }

            sr.Close();
            srac.Close();
            outp.Close();
        }
    }
}
